## Initialize workspace.
# Clear workspace.
rm(list = ls(all = TRUE))

# Set working directory to abp_replication directory.
setwd("~/Downloads/hbg_replication")

## Prepare output directory and main output files.
# If an output directory does not exist, create the directory.
if (!file.exists("output")) {
	dir.create("output")
}

# Create a log file for console output.
hbg_log <- file("output/hbg_log.txt", open = "wt")

# Echo and sink console log to psv_log file.
sink(hbg_log, append = TRUE)
sink(hbg_log, append = TRUE, type = "message")

## Replicate files and produce main output.
# Run abp_replication_code.R script, storing run-time statistics.
run_time <- system.time({source("scripts/hbg_cleaning.R", echo = TRUE, 
								max.deparse.length = 10000)
						 source("scripts/hbg_analysis.R", echo = TRUE,
						 	    max.deparse.length = 10000)})

# Close main output sink.
sink()
sink(type = "message")

## Sink run-time statistics to a run_time output file.
run_time_file <- file("output/run_time", open = "wt")
sink(run_time_file, append = TRUE)
print(run_time)
sink()
